package com.byzk.p2p.admin.borrow.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.byzk.p2p.admin.base.controller.AdminBaseController;
import com.byzk.p2p.admin.base.util.JsonMsg;
import com.byzk.p2p.admin.base.util.PageInfo;
import com.byzk.p2p.admin.borrow.dto.Product;
import com.byzk.p2p.admin.borrow.service.ProductService;

/**
 * 产品相关
 * @author liuchuan
 *
 */
@Controller
public class ProductController extends AdminBaseController{
	
	@Autowired
	private ProductService productService ;

	/**
	 * 产品列表
	 * @param model
	 * @param product
	 * @return
	 */
	@RequestMapping("/admin/product/list")
	public String listProduct(Model model,@ModelAttribute Product product){
		PageInfo<Product> pageInfo = productService.productList(product);
		model.addAttribute("pageInfo", pageInfo);
		return ftl(Thread.currentThread().getStackTrace());
	}
	
	/**
	 * 进入产品添加页面
	 * @return
	 */
	@RequestMapping("/admin/product/add")
	public String addProduct(){
		return ftl(Thread.currentThread().getStackTrace());
	}
	
	/**
	 * 产品添加或修改
	 * @param product
	 * @return
	 * @throws Exception
	 */
	
	@RequestMapping(value = "/admin/product/saveOrUpdate",method=RequestMethod.POST)
	public String saveOrUpdate(@ModelAttribute Product product) throws Exception{
		if(getUserId() == null){
			super.printJSON(new JsonMsg(1, "请先登录！"));
			return null; 
		}
		if(product.getName() == null){
			super.printJSON(new JsonMsg(2, "产品名不能为空！"));
			return null; 
		}
		if(product.getPercent() == null){
			super.printJSON(new JsonMsg(3, "年化不能为空！"));
			return null; 
		}
		if(product.getStatus() == null){
			product.setStatus("Y");
		}
		productService.saveOrUpadte(product);
		super.printJSON(new JsonMsg(-1, "操作成功！"));
		return null ; 
	}
	
	/**
	 * 进入产品修改页面
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping("/admin/product/update")
	public String update(Model model, @RequestParam Integer id){
		Product product = productService.selectById(id);
		model.addAttribute("product", product);
		return ftl(Thread.currentThread().getStackTrace());
	}
	
	/**
	 * 删除产品类型
	 * @param model
	 * @param id
	 * @throws Exception
	 */
	@RequestMapping(value="/admin/product/delete",method=RequestMethod.POST)
	public void deleteGood(Model model, @RequestParam("id") Integer id) throws Exception {
		try {
			productService.delete(id);
			super.printJSON(new JsonMsg(-1, "删除成功"));
		} catch (Exception e) {
			// TODO: handle exception
			super.printJSON(new JsonMsg(1, "删除失败"));
		}
	}
}
